package biz.yfsoft.app.aengine;

import com.alibaba.fastjson.JSONObject;

import biz.yfsoft.app.aengine.api.AEParam;
import biz.yfsoft.app.aengine.api.AERspType;
import biz.yfsoft.app.aengine.api.AEService;
import biz.yfsoft.app.aengine.utils.EntityUtils;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AE {

    public static final int MODE_DEV = 0;

    public static final int MODE_STAGING = 1;

    public static final int MODE_PRODUCT = 2;

    private static final String[] BASE_URLS = new String[]{
            "http://192.168.1.6:8080/",
            "http://api.sandbox.guoran100.com",
            "http://api.guoran100.com"
    };

    private AE(){}

    private static AE _me = new AE();

    public static AE me(){
        return _me;
    }

    private String _appkey;

    private String _masterkey;

    private int _mode = -1;

    private String _v = "0.0.1";

    private AEService _service;

    private AEService buildService(){
        return new Retrofit.Builder()
                .baseUrl(BASE_URLS[_mode])
                //.client(HttpUtils.client)
                .addConverterFactory(GsonConverterFactory.create(EntityUtils.gson))
                .build()
                .create(AEService.class);
    }

    public AE init(String appkey,String masterkey){
        _appkey = appkey;
        _masterkey = masterkey;
        return this;
    }

    public AE setMode (int mode){
        _mode = mode;
        _service = buildService();
        return this;
    }

    public int getMode(){
        return _mode;
    }

    public String getVersion(){
        return _v;
    }

    public AE setVersion(String v){
        _v = v;
        return this;
    }

    public AEService getService(){
        return _service;
    }

    public Call<AERspType> call(String method, AEParam param){


        return _service.call(method,_appkey,String.valueOf(System.currentTimeMillis()),param.toJSONString(),sign(),_v);
    }

    private String sign(){
        //TODO:完成参数签名
        return "";
    }

}
